package com.ruoyi.student.service.impl;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.logging.Logger;

import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.ClassCompareUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.student.domain.XzkInfoStudent;
import com.ruoyi.student.mapper.XzkInfoStudentMapper;
import com.ruoyi.student.mapper.XzkStudentPromotionMapper;
import com.ruoyi.system.mapper.SysDictDataMapper;
import com.ruoyi.system.mapper.SysDictTypeMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.student.mapper.XzkStudentMapper;
import com.ruoyi.student.domain.XzkStudent;
import com.ruoyi.student.service.IXzkStudentService;
import com.ruoyi.common.core.text.Convert;

/**
 * 学生信息Service业务层处理
 *
 * @author ruoyi
 * @date 2021-04-02
 */
@Service
public class XzkStudentServiceImpl implements IXzkStudentService {

    @Autowired
    private XzkStudentMapper xzkStudentMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private XzkInfoStudentMapper xzkInfoStudentMapper;

    @Autowired
    private SysDictDataMapper sysDictDataMapper;

    @Autowired
    private SysDictTypeMapper sysDictTypeMapper;

    @Autowired
    private XzkStudentPromotionMapper xzkStudentPromotionMapper;


    public static final String CourseTypeChange = "课程类型更改";
    public static final String StuPhoneChange = "课程手机号更改";
    public static final String GradeIdChange = "班级更改";
    public static final String StudyStatusChange = "学习状态更改";

    private List<String> changeList = Arrays.asList("coursetype", "stuphone", "studystatus","gradeid");


    /**
     * 查询学生信息
     *
     * @param stuid 学生信息ID
     * @return 学生信息
     */
    @Override
    public XzkStudent selectXzkStudentById(Long stuid) {
        return xzkStudentMapper.selectXzkStudentById(stuid);
    }

    /**
     * 查询学生信息
     *
     * @param stuname 学生信息ID
     * @return 学生信息
     */
    @Override
    public XzkStudent selectXzkStudentByName(String stuname) {
        return xzkStudentMapper.selectXzkStudentByName(stuname);
    }

    /**
     * excel查询学生信息列表
     *
     * @param xzkStudent 学生信息
     * @return 学生信息
     */
    @Override
    public List<XzkStudent> selectXzkStudentList(XzkStudent xzkStudent) {
        return xzkStudentMapper.selectXzkStudentList(xzkStudent);
    }

    /**
     * 查询学生信息列表
     *
     * @param xzkStudent 学生信息
     * @return 学生信息
     */
    @Override
    public List<XzkStudent> selectXzkStudentList1(XzkStudent xzkStudent,String username) {
        return xzkStudentMapper.selectXzkStudentList1(xzkStudent,username);
    }

    /**
     * 查询退休学学生信息列表
     *
     * @param xzkStudent 学生信息
     * @return 学生信息
     */
    @Override
    public List<XzkStudent> selectXzkStudentList2(XzkStudent xzkStudent,String userName) {
        return xzkStudentMapper.selectXzkDropStudentList2(xzkStudent,userName);
    }

    /**
     * 新增学生信息
     *
     * @param xzkStudent 学生信息
     * @return 结果
     */
    @Override
    public int insertXzkStudent(XzkStudent xzkStudent) {
        return xzkStudentMapper.insertXzkStudent(xzkStudent);
    }

    /**
     * 修改学生信息
     *
     * @param newXzkStudent 学生信息
     * @return 结果
     */
    @Override
    public int updateXzkStudent(XzkStudent newXzkStudent)
    {
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        XzkStudent oldXzkStudent = selectXzkStudentById(newXzkStudent.getStuid());
        XzkInfoStudent xzkInfoStudent = new XzkInfoStudent();
        Map<String, Map<String, Object>> stringMapMap = ClassCompareUtil.compareFields(newXzkStudent, oldXzkStudent);
        //日志信息有没有写入不影响当前
        boolean flag = Collections.disjoint(changeList, stringMapMap.keySet());

        if (!flag) {
            xzkInfoStudent.setSginphone(newXzkStudent.getStuphone());
            xzkInfoStudent.setOpt(newXzkStudent.getOpt());
            xzkInfoStudent.setStuid(newXzkStudent.getStuid());
            if (stringMapMap.keySet().contains("coursetype")) {
                Map<String, String> sysCourseType = listDictData("sys_course_type");
                xzkInfoStudent.setRemark(CourseTypeChange + ":" + sysCourseType.get(oldXzkStudent.getCoursetype()) + "更改为" + sysCourseType.get(newXzkStudent.getCoursetype()));

            }
            if (stringMapMap.keySet().contains("stuphone")) {
                xzkInfoStudent.setRemark(StuPhoneChange + ":" + oldXzkStudent.getStuphone() + "更改为" + newXzkStudent.getStuphone());
            }
            if (stringMapMap.keySet().contains("studystatus")) {
                Map<String, String> sysStudentStatus = listDictData("sys_student_status");
                String s = sysStudentStatus.get(newXzkStudent.getStudystatus());
                System.out.println(s);
                if ("学员休学".equals(sysStudentStatus.get(newXzkStudent.getStudystatus()))) {
                    xzkInfoStudent.setRemark(StudyStatusChange + ":" + sysStudentStatus.get(oldXzkStudent.getStudystatus()) + "更改为" + sysStudentStatus.get(newXzkStudent.getStudystatus()) + ",休学时间为:[" + format.format(newXzkStudent.getStartSuspend()) + "-" + format.format(newXzkStudent.getStopSuspend()) + "]");
                } else {
                    xzkInfoStudent.setRemark(StudyStatusChange + ":" + sysStudentStatus.get(oldXzkStudent.getStudystatus()) + "更改为" + sysStudentStatus.get(newXzkStudent.getStudystatus()));
                }
            }
            if (stringMapMap.keySet().contains("gradeid")) {
                xzkInfoStudent.setRemark(GradeIdChange + ":" + oldXzkStudent.getGradeid() + "更改为" + newXzkStudent.getGradeid());
            }
            xzkInfoStudentMapper.insertXzkInfoStudent(xzkInfoStudent);
        }

        return xzkStudentMapper.updateXzkStudent(newXzkStudent);
    }
    /**
     * 删除学生信息对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteXzkStudentByIds(String ids) {
        return xzkStudentMapper.deleteXzkStudentByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除学生信息信息
     *
     * @param stuid 学生信息ID
     * @return 结果
     */
    @Override
    public int deleteXzkStudentById(Long stuid) {
        return xzkStudentMapper.deleteXzkStudentById(stuid);
    }

    /*
     * 批量导入学生数据
     * */
    @Override
    public String importStudent(List<XzkStudent> studentList, boolean updateSupport, String operName) {
        if (StringUtils.isNull(studentList) || studentList.size() == 0) {
            throw new BusinessException("导入学生数据不能为空！");
        }
        Integer sucessNum = 0;
        Integer failesNum = 0;
        StringBuilder sucess = new StringBuilder();
        StringBuilder faile = new StringBuilder();

        //遍历学生集合

        for (XzkStudent xzkStudent : studentList) {
            try {
                if (updateSupport) {
                    //查找是否存在此学生
                    XzkStudent isExit = selectXzkStudentByName(xzkStudent.getStuname());
                    if (isExit == null || StringUtils.isEmpty(isExit.getStuname())) {
                    } else {
                        //如果存在那就更新
                        updateXzkStudent(xzkStudent);
                        sucessNum++;
                        sucess.append("<br/>" + xzkStudent.getStuname() + "学生更新成功");
                        continue;
                    }
                }
                //存入学生信息
                insertXzkStudent(xzkStudent);
                sucessNum++;
                sucess.append("<br/>" + xzkStudent.getStuname() + "学生导入成功");
            } catch (Exception e) {
                failesNum++;
                String msg = "<br/>" + xzkStudent.getStuname() + "学生导入失败：";
                faile.append(msg + e.getMessage());
            }
        }
        if (failesNum > 0) {
            faile.insert(0, "很抱歉，导入失败！共 " + failesNum + " 条数据格式不正确，错误如下：");
            throw new BusinessException(faile.toString());
        } else {
           sucess.insert(0, "恭喜您，数据已全部导入成功！共 " + sucessNum + " 条，数据如下：");
        }
        return sucess.toString();
    }

    /**
     * 查找学生信息信息
     *
     * @param stuids 学生信息ID
     * @return 结果
     */
    @Override
    public List<XzkStudent> selectXzkStudentByIds(String stuids) {
        return xzkStudentMapper.selectXzkStudentByIds(Convert.toStrArray(stuids));
    }

    public Map<String, String> listDictData(String type) {
        List<SysDictData> sysDictData = sysDictDataMapper.selectDictDataByType(type);
        Map<String, String> dictDataMap = new HashMap<>();
        for (SysDictData sysDictDatum : sysDictData) {
            dictDataMap.put(sysDictDatum.getDictValue(), sysDictDatum.getRemark());
        }
        return dictDataMap;
    }

    @Override
    public int superviseClass(Long stuid)   {
        return xzkStudentMapper.updateSuperviseClass(stuid,new Date());
    }

    @Override
    public void installXzkStudentByPromotion(Long stuid,String promotion) {

        xzkStudentPromotionMapper.installXzkStudentByPromotion(stuid, promotion, new Date());
    }

    @Override
    public List<XzkStudent> selectXzkStudentopenhistoryList(Long stuid) {

        return xzkStudentPromotionMapper.selectXzkStudentopenhistoryList(stuid);
    }

    @Override
    public int selectStuCountByMasterTeacher(String studystatus, String masterteacher,String teachername) {
        return xzkStudentMapper.selectStuCountByMasterTeacher(studystatus, masterteacher,teachername);
    }

}
